home *** CD-ROM | disk | FTP | other *** search
/ FM Towns: Free Software Collection 6 / FM Towns Free Software Collection 6.iso / t_os / chtiff / source / chtiff5.c < prev    next >
Text File  |  1993-07-08  |  10KB  |  378 lines

  1. /*
  2. ファイル名指定を良くするために、DOS専用関数 _splitpath を使用してます。
  3. MS-C のみに通用する関数かもしれませんので、代用として、__splitpath を用意
  4. してます。信頼度は_splitpath の方が上でしょう。
  5. */
  6.  
  7. #include<stdio.h>
  8. #include<stdlib.h>
  9. #include<ctype.h>
  10. #include<string.h>
  11. #include"define.h"
  12. #define MAIN 1
  13. #include"function.h"
  14.  
  15.  
  16. /* グローバル変数の宣言 */
  17. TIFF_INFO from,to;
  18. int kill_to;
  19.  
  20. void __splitpath(char *string, char *drive, char *dir, char *fname, char *ext)
  21. {
  22.     char *p;
  23.     int i;
  24.  
  25.     *drive = *dir = *fname = *ext = 0;
  26.  
  27.     if( isalpha(*string) && (*(string+1)==':') ){
  28.         *drive = *string;
  29.         *(drive+1) = ':';
  30.         *(drive+2) = 0;
  31.         string += 2;
  32.         }
  33.     else *drive = 0;
  34.  
  35.     *dir = 0;
  36.     while(1){
  37.         for( p = string; *p && (*p != '\\'); p++);
  38.         if( *p == 0 ) break;
  39.         while( string != p ){
  40.             *dir = *string;
  41.             dir++;
  42.             string++;
  43.             }
  44.         *dir = *string;
  45.         dir++;
  46.         string++;
  47.         }
  48.  
  49.     *dir = 0;
  50.  
  51.     for( p = string; *p && (*p != '.'); p++);
  52.     if( *p == 0 ){    /* 拡張子なし */
  53.         strcpy( fname, string );
  54.         *ext = 0;
  55.         }
  56.     else{    /* 拡張子あり */
  57.         while( string != p ){
  58.             *fname = *string;
  59.             fname++;
  60.             string++;
  61.             }
  62.     *fname = 0;
  63.  
  64.     /* 拡張子書き込み。a.b.c という記述にも対応するため、カウントしている */
  65.     for(i=0; i<4; i++){
  66.         if( *string == 0 ) break;
  67.         *ext = *string;
  68.         ext++;
  69.         string++;
  70.         }
  71.     *ext = 0;
  72.     }
  73. }
  74.  
  75.  
  76. main(int argc, char *argv[])
  77. {
  78.     unsigned long offset;
  79.     int i, j, from_file, to_file, del, Intel, stop, expand;
  80.     char string[2][256];
  81.     char drive[2][3];
  82.     char dir[2][256];
  83.     char fname[2][9];
  84.     char ext[2][5];
  85.     char ren[256];
  86.  
  87.     from_file = to_file = 0;
  88.     expand = 0;
  89.     kill_to = 0;
  90.     to.Compression = 1;
  91.     del = 0;
  92.     stop = 1;
  93.     Intel = 2;
  94.     to.Bits_Per_Sample = 0;
  95.     to.Rows_Per_Strip = 0;
  96.     ren[0] = 0;
  97.     string[0][0] = string[1][0] = 0;
  98.  
  99.     for(i=1; i<argc; i++){
  100.         if( (*argv[i]=='/')||(*argv[i]=='-') ) goto OPTION;
  101.  
  102.         if( from_file == 0 ){ from_file = i; continue;}
  103.         if( to_file == 0 ) to_file = i;
  104.         continue;    /* 2つ以上のファイル指定は無視 */
  105.  
  106.         OPTION:
  107.         switch( *(argv[i]+1) ){
  108.             case'c': case'C':
  109.                 to.Compression = 5;
  110.                 continue;
  111.  
  112.             case'd': case'D':
  113.                 switch( *(argv[i]+2) ){
  114.                     case'x': case'X':
  115.                         to.X_Resolution.bunsi = atol( argv[i]+3 );
  116.                         continue;
  117.                     case'y': case'Y':
  118.                         to.Y_Resolution.bunsi = atol( argv[i]+3 );
  119.                         continue;
  120.                     default:
  121.                         to.X_Resolution.bunsi = to.Y_Resolution.bunsi =
  122.                         atol( argv[i]+2 );
  123.                         }
  124.                 continue;
  125.  
  126.             case'b': case'B':
  127.                 j = atoi( argv[i]+2 );
  128.                 if( ( j%8 )||( j<8 )||( 24<j) ){
  129.                     printf("この変換には対応してません。%d",j);
  130.                     usage();
  131.                     exit(1);
  132.                     }
  133.                 to.Bits_Per_Sample = j;
  134.                 continue;
  135.  
  136.             case'i': case'I':
  137.                 Intel = 1;
  138.                 continue;
  139.  
  140.             case'm': case'M':
  141.                 Intel = 0;
  142.                 continue;
  143.  
  144.             case's': case'S':
  145.                 j = atoi( argv[i]+2 );
  146.                 if(j<=0){
  147.                     puts("Sオプション指定が間違ってます。");
  148.                     usage();
  149.                     exit(1);
  150.                     }
  151.                 to.Rows_Per_Strip = j;
  152.                 continue;
  153.  
  154.             case'n': case'N':
  155.                 stop = 0;
  156.                 continue;
  157.  
  158.             case'e': case'E':
  159.                 expand = 1;
  160.                 continue;
  161.  
  162.             default:
  163.                 usage();
  164.                 exit(0);
  165.             }
  166.         }
  167.  
  168.     /* 拡張子省略・ファイル名設定なしなどに対応する。*/
  169.     if( from_file == 0 ){
  170.         fputs("変換元ファイル名が、指定されてません\n",stderr);
  171.         usage();
  172.         exit(1);
  173.         }
  174.     strcpy(string[0],argv[from_file]);
  175.     __splitpath(string[0], drive[0], dir[0], fname[0], ext[0]);
  176.     if( *fname[0] == 0 ){
  177.         fputs("変換元ファイル名が、指定されてません\n",stderr);
  178.         usage();
  179.         exit(1);
  180.         }
  181.     if( *ext[0] == 0 ) strcat(string[0],".tif");
  182.  
  183.     /* 拡張子省略・変換先設定なしなどに対応 */
  184.     if( to_file == 0 ){
  185.         sprintf(string[1],".\\%s.@",fname[0]);    /* 同名をカレントに作成 */
  186.         ext[1][0] = 0;
  187.         }
  188.     else{
  189.         strcpy( string[1], argv[ to_file ] );
  190.         __splitpath(string[1], drive[1], dir[1], fname[1], ext[1]);
  191.         if( *fname[1] == 0 ){
  192.             if( (drive[1]==0) && (dir[1]==0) ){
  193.                 fputs("変換先ファイル名が、指定されてません\n",stderr);
  194.                 usage();
  195.                 exit(1);
  196.                 }
  197.             sprintf(string[1],"%s%s%s.@",drive[1],dir[1],fname[0]);
  198.             strcpy(ext[1],".tif");
  199.             }
  200.         else{
  201.             sprintf(string[1],"%s%s%s.@",drive[1],dir[1],fname[1]);
  202.             }
  203.         }
  204.     if( *ext[1] == 0 ) strcpy( ext[1], ".tif" );
  205.         
  206.  
  207.     if( NULL==(from.fp=fopen(string[0],"rb")) ){
  208.         fprintf(stderr,"変換元 %s が見つかりません。\n",string[0]);
  209.         usage();
  210.         exit(1);
  211.         }
  212.  
  213.     if( isIntel() == -1 ){
  214.         fprintf(stderr,"%sは TIFF file ではないようです。",argv[from_file]);
  215.         exit(1);
  216.         };
  217.  
  218.     offset = read_LONG(0L);
  219.     fseek(from.fp, offset, SEEK_SET);
  220.     read_IFD();
  221.  
  222.     if(from.Intel) puts("Intel形式です。");
  223.               else puts("モトローラ形式です。");
  224.  
  225.     printf("%8lu : New Subfile Type\n",from.New_Subfile_Type);
  226.     printf("%8u : Image Width\n",from.Image_Width);
  227.     printf("%8u : Image Length\n",from.Image_Length);
  228.     printf("%8u : Bits Per Pixel",from.Bits_Per_Sample);
  229.     if(from.Opacity) puts("(with opacity)"); else puts("");
  230.     printf("%8u : Compression\n",from.Compression);
  231.     printf("%8u : Photo Interp\n",from.Photo_Interp);
  232.     printf("%8u : Fill order\n",from.Fill_Order);
  233.  
  234.     if( from.Strip_Offset == NULL )
  235.         puts("-------- : Strip_Offset");
  236.     else
  237.         printf("%08lx : Strip Offset\n",*from.Strip_Offset);
  238.  
  239.     printf("%8u : Samples Per Pixel\n",from.Samples_Per_Pixel);
  240.     printf("%8lu : Rows Per Strip\n",from.Rows_Per_Strip);
  241.  
  242.     if( from.Strips_ByteCount == NULL )
  243.         puts("-------- : Strips ByteCount");
  244.     else
  245.         printf("%8lu : Strips ByteCount\n",*from.Strips_ByteCount);
  246.  
  247.     printf("%8u : Max Sample Value\n",from.Max_Sample_Value);
  248.     printf("%4lu/%4lu: X Resolution\n",from.X_Resolution.bunsi,from.X_Resolution.bunbo);
  249.     printf("%4lu/%4lu: Y Resolution\n",from.Y_Resolution.bunsi,from.Y_Resolution.bunbo);
  250.     printf("%8u : Planer Config\n",from.Planer_Config);
  251.     printf("%8d : Color Map\n",from.Color_Map);
  252.     
  253. /*    for(i=0; i<from.Strips; i++)
  254.         printf("Strip %d: offset %08lx  length %08x\n",
  255.             i,*(from.Strip_Offset+i),*(from.Strips_ByteCount+i) ); */
  256.  
  257.     /* 今のところ、JPEG等には非対応 */
  258.     if( ( from.Compression != 1 ) && ( from.Compression !=5 ) ){
  259.         fputs("LZW圧縮以外の圧縮には、対応してません。\n",stderr);
  260.         exit(1);
  261.         };
  262.  
  263.     /* 書き込みファイルの属性決定 */
  264.     if( Intel == 2 ) to.Intel = from.Intel;
  265.                 else to.Intel = Intel;
  266.     to.Opacity = 0;
  267.     to.Strips = 0;
  268.     to.New_Subfile_Type = 0;
  269.     to.Image_Width = from.Image_Width;
  270.     if(expand) to.Image_Width += from.Image_Width / 3;
  271.     to.Image_Length = from.Image_Length;
  272.     if(to.Bits_Per_Sample == 0) to.Bits_Per_Sample = from.Bits_Per_Sample;
  273.     switch(to.Bits_Per_Sample){
  274.         case  8:
  275.             to.Photo_Interp = 3;
  276.             to.Samples_Per_Pixel = 1;
  277.             break;
  278.         case 16:
  279.             to.Photo_Interp = 1;
  280.             to.Samples_Per_Pixel = 1;
  281.             break;
  282.         case 24:
  283.             to.Photo_Interp = 2;
  284.             to.Samples_Per_Pixel = 3;
  285.             }
  286.     to.Fill_Order = 1;
  287.  
  288.     if( to.Bits_Per_Sample == 16 )
  289.         to.Max_Sample_Value = (1<<15)-1;
  290.     else
  291.         to.Max_Sample_Value = (1<<to.Bits_Per_Sample)-1;
  292.  
  293.     /* 解像度のチェックをしないと、NeXTなどで誤動作をまねきます */
  294.     if( to.X_Resolution.bunsi == 0 || to.X_Resolution.bunbo == 0 )
  295.         if( from.X_Resolution.bunsi && from.X_Resolution.bunbo ){
  296.             to.X_Resolution.bunsi = from.X_Resolution.bunsi;
  297.             to.X_Resolution.bunbo = from.X_Resolution.bunbo;
  298.             }
  299.         else{
  300.             to.X_Resolution.bunsi = 72UL;
  301.             to.X_Resolution.bunbo = 1UL;
  302.             }
  303.  
  304.     if( to.Y_Resolution.bunsi == 0 || to.Y_Resolution.bunbo == 0 )
  305.         if( from.Y_Resolution.bunsi && from.Y_Resolution.bunbo ){
  306.             to.Y_Resolution.bunsi = from.Y_Resolution.bunsi;
  307.             to.Y_Resolution.bunbo = from.Y_Resolution.bunbo;
  308.             }
  309.         else{
  310.             to.Y_Resolution.bunsi = 72UL;
  311.             to.Y_Resolution.bunbo = 1UL;
  312.             }
  313.  
  314.     to.Planer_Config = 1;
  315.     to.Color_Map = 0;
  316.  
  317.     if( to.Rows_Per_Strip == 0 )
  318.       /* Townsの16ビットはストライプ分割を認めない、意固地だそうな */
  319.       if( ( to.Compression==1)||( to.Bits_Per_Sample == 16 ) )
  320.         to.Rows_Per_Strip = to.Image_Length;
  321.       else
  322.         to.Rows_Per_Strip = (TEMP_SIZE)/(to.Image_Width*to.Bits_Per_Sample/8L);
  323.  
  324.     j = (to.Image_Length / to.Rows_Per_Strip) + 1;
  325.     to.Strips_ByteCount=MALLOC( 4UL*j );
  326.     to.Strip_Offset=MALLOC( 4UL*j );
  327.  
  328.     printf("\n[ %s ] → [ %s ] の変換です。\n",string[0],string[1]);
  329.     puts("変換先のファイルの書式は");
  330.     if( to.Compression == 5 ) fputs("LZW圧縮/",stdout);
  331.     printf("%dビット/1ストライプ %d ライン/",to.Bits_Per_Sample, to.Rows_Per_Strip);
  332.     if(expand) fputs("幅拡張/",stdout);
  333.     if(to.Intel == 0 ) puts("モトローラ式");
  334.                   else puts("インテル式");
  335.  
  336.     if( stop ){
  337.         puts("です。リターンで開始します。");
  338.         fgetc(stdin);
  339.         }
  340.     else puts("です。");
  341.  
  342.     if( NULL == (to.fp = fopen(string[1],"wb")) ){
  343.         fprintf(stderr,"ファイル %s の作成に失敗しました。\n",string[1]);
  344.         exit(1);
  345.         };
  346.  
  347.     if(to.Intel) write_SHORT(0UL,0x4949);    /* intel形式 */
  348.             else write_SHORT(0UL,0x4d4d);    /* モトローラ形式 */
  349.  
  350.     write_SHORT(0UL,42);    /* バージョン */
  351.     write_LONG(0UL,0UL);    /* IFD offset のダミー値 */
  352.  
  353.     /* さて、グラフィックデータの書き込みっと・・・ */
  354.     switch(from.Bits_Per_Sample){
  355.         case 8 : change8(expand) ; break;
  356.         case 12: change12(expand); break;
  357.         case 16: change16(expand); break;
  358.         case 24: change24(expand); break;
  359.         case 32: change32(expand); break;
  360.         default: fputs("この変換には対応してません。\n",stderr);
  361.                  exit(1);
  362.         };
  363.  
  364.     /* 残るはIFDだ! */
  365.     puts("IFD を書き込みます。");
  366.     write_IFD();
  367.  
  368.     puts("変換は終了しました。");
  369.     fclose(to.fp);
  370.  
  371.     __splitpath( string[1], drive[1], dir[1], fname[1], ext[0]);
  372.     *ren = 0;
  373.     sprintf(ren,"%s%s%s%s",drive[1],dir[1],fname[1],ext[1]);
  374.     if( NULL == fopen( ren,"rb") ) rename(string[1],ren);
  375.     else printf("Rename できなかったので、[ %s ]のまま残します。\n",string[1]);
  376. }
  377. 
  378.